package com.guat.servlet.picture;

import com.google.gson.Gson;
import com.guat.entity.Picture;
import com.guat.service.PictureService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/searchPicture")
public class SearchServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置前端返回数据使用UTF-8编码,防止乱码
        req.setCharacterEncoding("UTF-8");
        //获取前端传递的参数
        String value = req.getParameter("value");

        PictureService pictureService = new PictureService();
        List<Picture> list = pictureService.searchPicture(value);

        //设置返回前端数据使用UTF-8编码,防止乱码
        resp.setCharacterEncoding("UTF-8");
        //设置响应内容为json,以便前端正确解析返回的数据
        resp.setContentType("application/json");
        //将Map集合转换为json
        Gson gson = new Gson();
        String mapJson = gson.toJson(list);
        //将数据写入输入流中
        resp.getWriter().write(mapJson);
    }
}
